// pages/main_shoping/main_shoping.js
import requestHttp from '../../utils/Http.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goods: 0,
    selectarry: [],
    cart: [],
    selectarr: [],
    selectall: false,
    cart_list: [],
    goods_price: 0,
    goods_num: '',
    checkevery: true,
    checkevery_num: 0,
    selectsomething: [],
    resultselectsomething: [],
    shopping: 1





  },





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.request({
      url: 'https://ds.wyadmin.cn/api/Membercart/cart_list',
      method: 'GET',
      data: {

      },
      header: {

        'X-DS-KEY': wx.getStorageSync('token'),
        'content-type': 'application/json',

      },
      success: res => {
        const product = res.data.result
        const cart_list = product.cart_list

        for (var index in cart_list) {
          cart_list[index].checkall = true


          //   for (var i = 0; i < cart_list[index].goods.length; i++) {
          //     console.log(cart_list[index].goods[i])
          // // .checked = true
          // cart_list[index].goods[i] = { ...cart_list[index].goods[i], checked: true}
          //   }
        }
        this.setData({
          cart_list,
        })

      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  selectAll: function(e) {
    // const {
    //   storeIndex,

    // } = e.currentTarget.dataset.index;

    let cart_list = this.data.cart_list;
    // let selectarr = this.data.selectarr;
    var index = e.currentTarget.dataset.index
    console.log("index", e.currentTarget.dataset.index)
    cart_list[index].checkall = !cart_list[index].checkall

    if (cart_list[index].checkall == true) {

      for (var i = 0; i < cart_list[index].goods.length; i++) {
        cart_list[index].goods[i].storage_state = true;
        cart_list[index].checkall = true
      }

    } else {

      for (var i = 0; i < cart_list[index].goods.length; i++) {
        cart_list[index].goods[i].storage_state = false;
        cart_list[index].checkall = false
      }

    }
    //   for (var i = 0; i < cart_list[index].goods.length; i++) {
    //     cart_list[index].goods[i].storage_state = true;
    //     selectarr.push(cart_list[index].goods[i])
    //   }
    //   console.log(cart_list[storeIndex].peroductnumber)
    // } else {
    //   for (var i = 0; i < cart_list[storeIndex].goods.length; i++) {
    //     cart_list[storeIndex].goods[i].storage_state = false
    //   }
    //   selectarr = []
    // }
    this.setData({
      cart_list,

      // selectarr
    })

    // const {
    //  storeIndex
    // } = e.target.dataset;
    // let cart_list = this.data.cart_list;
    // let selectarry = this.data.selectarry;
    // console.log(e)
    // console.log(cart_list[1].checkall)
    // cart_list[storeIndex].checkall = !cart_list[storeIndex].checkall;
    // console.log(cart_list[storeIndex].checkall)
    // this.setData({
    //   selectall
    // })
    // if (cart_list[sIndex].checkall = true) {

    // }
    // if (selectall) {
    //   for (var i = 0; i < cart_list[storeIndex].goods.length; i++) {
    //     cart_list[storeIndex].goods[i] = true
    //     selectarry.push(cart_list[storeIndex].goods[i])
    //   }
    // } else {
    //   for (var i = 0; i < cart_list[sIndex].goods.length; i++) {
    //     cart_list[storeIndex].goods[i] = false
    //   }
    //   selectall = []
    // }

  },
  serviceSelection: function(e) {
    const {
      index,
      storeIndex,

    } = e.currentTarget.dataset;

    console.log(e)
    let cart_list = this.data.cart_list;
    let storecount = this.data.storecount;
    let selectpudect = this.data.selectpudect;
    cart_list[storeIndex].goods[index].storage_state = !cart_list[storeIndex].goods[index].storage_state;
    for (var k in cart_list) {
      cart_list[k].selectpudect = 0
      for (var i = 0; i < cart_list[k].goods.length; i++) {
        if (cart_list[k].goods[i].storage_state == true) {

          cart_list[k].selectpudect = cart_list[k].selectpudect + 1

          if (cart_list[k].goods[i].length == cart_list[k].selectpudect) {
            cart_list[k].goods[i].length = cart_list[k].selectpudect

          }
        }

      }

    }

    this.setData({
      cart_list,
    })


  },

  numberMinus: function(e) {
    let cart_list = this.data.cart_list;
    let goods_price = this.data.goods_price;
    let goods_num = this.data.goods_num;
    let index = e.currentTarget.dataset.index
    let storeIndex = e.currentTarget.dataset.storeIndex
    if (cart_list[storeIndex].goods[index].storage_state == true) {
      if (cart_list[storeIndex].goods[index].goods_num <= 1) {
        return
      } else {
        cart_list[storeIndex].goods[index].goods_num--
      }
      goods_price = cart_list[storeIndex].goods[index].goods_price * 1 * cart_list[storeIndex].goods[index].goods_num
    }
    this.setData({
      cart_list,
      goods_price,




    })

    // console.log(e)
  },
  numberAdd: function(e) {
    let cart_list = this.data.cart_list;
    let goods_price = this.data.goods_price;
    let goods_num = this.data.goods_num;
    let index = e.currentTarget.dataset.index
    let storeIndex = e.currentTarget.dataset.storeIndex
    if (cart_list[storeIndex].goods[index].storage_state == true) {
      if (cart_list[storeIndex].goods[index].goods_num >= cart_list[storeIndex].goods[index].goods_storage) {
        return
      } else {
        cart_list[storeIndex].goods[index].goods_num++
      }
      goods_price = goods_price + cart_list[storeIndex].goods[index].goods_price * 1 * cart_list[storeIndex].goods[index].goods_num

    }
    this.setData({
      cart_list,

    })
  },

  totalPrice: function(e) {
    let resultselectsomething = this.data.resultselectsomething
    let selectsomething = this.data.selectsomething
    let cart_list = this.data.cart_list
    let goods_price = this.data.goods_price
    let index = e.currentTarget.dataset.index
    let storeIndex = e.currentTarget.dataset.storeIndex
    console.log(e)
    cart_list[storeIndex].goods[index].storage_state = !cart_list[storeIndex].goods[index].storage_state
    console.log(cart_list[storeIndex].goods[index].storage_state)
    for (let k in cart_list) {
      cart_list[k].peroductnumber = 0
      for (var i = 0; i < cart_list[k].goods.length; i++) {
        if (cart_list[k].goods[i].storage_state == true) {
          selectsomething.push([cart_list[k].goods[i].cart_id, cart_list[k].goods[i].goods_num])

          cart_list[k].peroductnumber = cart_list[k].peroductnumber + 1
          goods_price = goods_price + cart_list[k].goods[i].goods_price * 1 * cart_list[k].goods[i].goods_num
          if (cart_list[k].peroductnumber == cart_list[k].goods[i].length) {
            cart_list[k].peroductnumber == cart_list[k].goods[i].length


          } else {
            cart_list[k].checkall = false
            selectsomething: []
          }
        } else {
          cart_list[k].goods[i].storage_state == false
          // goods_price = 0
          // goods_price = goods_price - cart_list[k].goods[i].goods_price 
          // goods_price = goods_price - cart_list[storeIndex].goods[index].goods_price * 1 * cart_list[storeIndex].goods[index].goods_num
        }

      }


    }
    this.setData({
      cart_list,
      goods_price,

    })
  },
  allOf: function(e) {
    let checkevery_num = this.data.checkevery_num

    let checkevery = this.data.checkevery
    let cart_list = this.data.cart_list
    checkevery = !checkevery
    for (var k in cart_list) {
      if (cart_list[k].checkall) {
        checkevery = true
      } else {
        checkevery = false
      }
    }
    console.log(cart_list[k].checkall)
    this.setData({
      cart_list,
      checkevery,

    })
  },
  account: function() {
  //  requestHttp('https://ds.wyadmin.cn/api/Membercart/cart_list', 'GET', '', {
  //     'X-DS-KEY': wx.getStorageSync('token')
  //   })
    let shopping = this.data.shopping
    let selectsomething = this.data.selectsomething
    let resultselectsomething = this.data.resultselectsomething
    const newResultselectsomething = [];
    // console.log('未遍历商品+店铺id',selectsomething)
    for (var i = 0; i < selectsomething.length; i++) {
      // console.log('商品+店铺id',selectsomething[i])

      newResultselectsomething.push(selectsomething[i].join('|'));
      // console.log('join后商品+店铺id',newResultselectsomething)
    }
    resultselectsomething = newResultselectsomething.join(',')


    wx.setStorageSync(selectsomething, selectsomething)
    wx.navigateTo({
      url: '/pages/affirm/affirm?resultselectsomething=' + resultselectsomething + '&shopping=' + shopping,
    })
  }
})

// function 
// 循环每个店铺
// ---循环每个商品
// ---选中几个=>店铺 ?= 商品数量 每个店铺选中的商品数量
// 一共选中几个
// 一共几件商品 == 一共选中几个
// ----商品*数量 = 总价